package com.aijia.custom.product.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author yejianjun
 */
public class SharedPreferencesUtils {

    private static final String MY_SETTTING_PREFERENCES = "aj_settting_share_preferences";
    /**
     * 是否为第一次打开app
     */
    private static final String APPFIRSTOPEN_PREFERENCES = "app_first_open_preferences";
    private static SharedPreferences mSettingPreferences;
    private static final String AUTO_LOGIN_PREFERENCES = "auto_login_preferences";
    private static final String USER_NAME_PREFERENCES = "user_name_preferences";
    private static final String PASSWORD_PREFERENCES = "password_preferences";
    private static final String TOKEN_PREFERENCES = "token_preferences";
    private static final String CDNTOKEN_PREFERENCES = "cdntoken_preferences";
    private static final String SHARE_PREFERENCE_SOFT_INPUT_HEIGHT = "soft_input_height";
    private static final String SHARE_PREFERENCE_CLIENTID = "getui_clientId";
    private static final String SHARE_USER_TYPE = "user_type_preferences";
    private static final String USER_ID = "user_id_preferences";
    private static final String ORGANIZENAME = "organizeName_preferences";
    private static final String ORGANIZEID = "organizeId_preferences";


    public static void init(Context context) {
        mSettingPreferences = context.getSharedPreferences(MY_SETTTING_PREFERENCES, Activity.MODE_PRIVATE);
    }

    public static void relese() {
        mSettingPreferences = null;
    }

    public static void clear() {
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(APPFIRSTOPEN_PREFERENCES, null);
        editor.putString(AUTO_LOGIN_PREFERENCES, null);
        editor.commit();
    }

    @SuppressLint("NewApi")
    public static boolean getIsFirstOpen(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getBoolean(APPFIRSTOPEN_PREFERENCES, true);
    }

    @SuppressLint("NewApi")
    public static void setIsFirstOpen(Context context, boolean isFirstOpen) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putBoolean(APPFIRSTOPEN_PREFERENCES, isFirstOpen);
        editor.commit();
    }

    /**
     * 设置自动登录状态
     *
     * @param auto
     */
    public static void setAutoLoginStatus(Context context, boolean auto) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putBoolean(AUTO_LOGIN_PREFERENCES, auto);
        editor.commit();
    }


    /**
     * 获取自动登录状态
     *
     * @return
     */
    public static boolean getAutoLoginStatus(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getBoolean(AUTO_LOGIN_PREFERENCES, false);
    }

    /**
     * 设置登录用户名
     *
     * @param organizeName
     */
    public static void setOrganizeName(Context context, String organizeName) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(ORGANIZENAME, organizeName);
        editor.commit();
    }


    /**
     * 获取登录用户名
     *
     * @return
     */
    public static String getOrganizeName(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(ORGANIZENAME, "");
    }

    /**
     * 设置登录用户ID
     *
     * @param organizeId
     */
    public static void setOrganizeId(Context context, int organizeId) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putInt(ORGANIZEID, organizeId);
        editor.commit();
    }


    /**
     * 获取登录用户ID
     *
     * @return
     */
    public static int getOrganizeId(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getInt(ORGANIZEID, 0);
    }


    /**
     *设置用户ID
     *
     * @param username
     */
//	public static void setAccountId(String accountId){
//		SharedPreferences.Editor editor = mSettingPreferences.edit();
//		editor.putString(USER_ACCOUTID_PREFERENCES, accountId);
//		editor.commit();
//	}

    /**
     * 获取用户ID
     *
     * @return
     */
//	public  static String getAccountId(){
//		return mSettingPreferences.getString(USER_ACCOUTID_PREFERENCES, "");
//	}

    /**
     * 设置用户名，登录后台时的手机号码
     *
     * @param context
     * @param username
     */
    public static void setUserName(Context context, String username) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(USER_NAME_PREFERENCES, username);
        editor.commit();
    }

    /**
     * 获取用户名，登录后台时的手机号码
     *
     * @param context
     * @return
     */
    public static String getUserName(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(USER_NAME_PREFERENCES, "");
    }

    /**
     * 设置密码
     *
     * @param context
     * @param password
     */
    public static void setPassword(Context context, String password) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(PASSWORD_PREFERENCES, password);
        editor.commit();
    }

    /**
     * 获取密码
     *
     * @return
     */
    public static String getPassword(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(PASSWORD_PREFERENCES, "");
    }

    /**
     * @param context
     * @param id
     */
    public static void setUserID(Context context, int id) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putInt(USER_ID, id);
        editor.commit();
    }

    public static int getUserID(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getInt(USER_ID, 0);
    }

    /**
     * @param context
     * @param userType 1.店长，2.老板，3.工人
     */
    public static void setUserType(Context context, int userType) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putInt(SHARE_USER_TYPE, userType);
        editor.commit();
    }

    public static int getUserType(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getInt(SHARE_USER_TYPE, 0);
    }


    /**
     * @param context
     * @param token
     */
    public static void setToken(Context context, String token) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(TOKEN_PREFERENCES, token);
        editor.commit();
    }

    public static String getToken(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(TOKEN_PREFERENCES, "");
    }

    public static void setCdnToken(Context context, String cdnToken) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(CDNTOKEN_PREFERENCES, cdnToken);
        editor.commit();
    }

    public static String getCdnToken(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(CDNTOKEN_PREFERENCES, "");
    }

    /**
     * 获取个推ID
     *
     * @param context
     * @return
     */
    public static String getClientId(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(SHARE_PREFERENCE_CLIENTID, null);
    }

    /**
     * 设置个推ID
     *
     * @param context
     * @param clientId
     */
    public static void setClientId(Context context, String clientId) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(SHARE_PREFERENCE_CLIENTID, clientId);
        editor.commit();
    }


}
